//
//  UIView.swift
//  yzb
//
//  Created by cyptt on 2021/12/9.
//

import UIKit

extension UIView {
  
   
    /// 裁剪 view 的圆角
    public func setRectCorner(direction: UIRectCorner, cornerRadius: CGFloat) {
        let cornerSize = CGSize(width: cornerRadius, height: cornerRadius)
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: direction, cornerRadii: cornerSize)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = maskPath.cgPath
        layer.addSublayer(maskLayer)
        layer.mask = maskLayer
    }
     public func bgClickColor(){
 
        let view:UIView = UIView.init(frame: self.bounds)
        self.addSubview(view)
          view.backgroundColor = UIColor.init(red: 230/255.0, green: 230/255.0, blue: 230/255.0, alpha: 1)
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now()+0.1) {
              //此时处于主队列中
            DispatchQueue.main.async {
                view.removeFromSuperview()
             
         
            }
        }
    }
    
    /*
     添加底部安全view  --一般在视图加载完显示
     @param safeView 父亲视图
    
     */
    public func addBottomSafeView(safeView:UIView)->UIView{

        let v = UIView()
        v.backgroundColor = UIColor.white
        safeView.addSubview(v)
        v.frame = CGRect.init(x: 0, y: self.frame.maxY, width: self.frame.size.width, height:  self.frame.size.height)
        return v
    }
  
    /**
     添加底部线条
     */
 
    public func addBottomBorder(color: UIColor, lineH: CGFloat,marginL:CGFloat = 0,margingR:CGFloat = 0) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = CGRect(x: marginL, y: self.frame.size.height - lineH, width: self.frame.size.width-marginL-margingR, height: lineH)
        self.layer.addSublayer(border)
    }
    
    
    /**
     获取当前view到控制器
     */
    public  var parentViewController: UIViewController? {
           var parentResponder: UIResponder? = self
           while parentResponder != nil {
               parentResponder = parentResponder!.next
               if let viewController = parentResponder as? UIViewController {
                   return viewController
               }
           }
           return nil
    }
    
    /**
     添加虚线
     */
    public func addDashedBorder(color: UIColor = .black,
                        lineWidth: CGFloat = 1,
                        dashPattern: [NSNumber] = [5, 5],
                        cornerRadius: CGFloat = 0) {
        
        // 移除已存在的虚线边框
        removeDashedBorder()
        
        let dashedBorder = CAShapeLayer()
        dashedBorder.name = "dashed-border"
        dashedBorder.strokeColor = color.cgColor
        dashedBorder.lineDashPattern = dashPattern
        dashedBorder.frame = bounds
        dashedBorder.fillColor = nil
        dashedBorder.lineWidth = lineWidth
        
        // 处理圆角
        if cornerRadius > 0 {
            dashedBorder.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
        } else {
            dashedBorder.path = UIBezierPath(rect: bounds).cgPath
        }
        
        layer.addSublayer(dashedBorder)
    }
    
    func removeDashedBorder() {
        layer.sublayers?
            .filter { $0.name == "dashed-border" }
            .forEach { $0.removeFromSuperlayer() }
    }
}
